package com.sjsu.cloudmanager.server.service;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sjsu.cloudmanager.client.service.CloudUserAdminService;
import com.sjsu.cloudmanager.client.ui.model.CloudUserBean;
import com.sjsu.cloudmanager.dao.CloudUserDao;
import com.sjsu.cloudmanager.model.CloudUser;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ckempaiah
 * Date: 10/21/11
 * Time: 9:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class CloudUserServiceImpl extends RemoteServiceServlet implements CloudUserAdminService {

    private CloudUserDao cloudUserDao;

    @Override
    public List<CloudUserBean> getCloudUsers() {
        List<CloudUser> cloudUserList = cloudUserDao.findCloudUserList();
        List<CloudUserBean> cloudUserBeanList = new ArrayList<CloudUserBean>();
        if (CollectionUtils.isNotEmpty(cloudUserList)) {
            for (CloudUser cloudUser : cloudUserList) {
                CloudUserBean cloudUserBean = new CloudUserBean();
                cloudUserBean.setUserId(cloudUser.getUserId());
                cloudUserBean.setUserName(cloudUser.getUserName());
                cloudUserBean.setUserFirstName(cloudUser.getUserFirstName());
                cloudUserBean.setUserLastName(cloudUser.getUserLastName());
                cloudUserBean.setUserEmail(cloudUser.getUserEmail());
                cloudUserBean.setUserCreatedDate(cloudUser.getUserCreatedDate());
                cloudUserBean.setUserUpdatedDate(cloudUser.getUserUpdatedDate());
                cloudUserBean.setActive(cloudUser.getActive());
                cloudUserBeanList.add(cloudUserBean);
            }
        }
        return cloudUserBeanList;
    }

    @Override
    public void saveCloudUser(CloudUserBean cloudUserBean) {
        CloudUser cloudUser = null;
        if (cloudUserBean.getUserId() != null && cloudUserBean.getUserId() > 0) {
            cloudUser = cloudUserDao.getCloudUser(cloudUserBean.getUserId());

        } else {
            cloudUser = new CloudUser();
            cloudUser.setUserCreatedDate(new Date());
            cloudUser.setAdministrator(false);
            cloudUser.setUserName(cloudUserBean.getUserName());
        }
        cloudUser.setUserFirstName(cloudUserBean.getUserFirstName());
        cloudUser.setUserLastName(cloudUserBean.getUserLastName());
        cloudUser.setUserPassword(cloudUserBean.getUserPassword());
        cloudUser.setUserEmail(cloudUserBean.getUserEmail());
        cloudUser.setUserUpdatedDate(new Date());
        cloudUser.setActive(cloudUserBean.getActive());
        //cloudUser.setActive(true);
        cloudUserDao.addUpdateCloudUser(cloudUser);
    }

    @Override
    public CloudUserBean getCloudUser(Integer cloudUserId) {
        CloudUser cloudUser = cloudUserDao.getCloudUser(cloudUserId);

        CloudUserBean cloudUserBean = new CloudUserBean();

        cloudUserBean.setUserId(cloudUser.getUserId());
        cloudUserBean.setUserName(cloudUser.getUserName());
        cloudUserBean.setUserFirstName(cloudUser.getUserFirstName());
        cloudUserBean.setUserLastName(cloudUser.getUserLastName());
        cloudUserBean.setUserEmail(cloudUser.getUserEmail());
        cloudUserBean.setUserCreatedDate(cloudUser.getUserCreatedDate());
        cloudUserBean.setUserUpdatedDate(cloudUser.getUserUpdatedDate());
        cloudUserBean.setActive(cloudUser.getActive());
        return cloudUserBean;
    }

    public CloudUserDao getCloudUserDao() {
        return cloudUserDao;
    }

    public void setCloudUserDao(CloudUserDao cloudUserDao) {
        this.cloudUserDao = cloudUserDao;
    }
}
